package crm.view.setting.addremovelist;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import crm.model.teacherPC.Manager;
import crm.model.teacherPC.bannedList.Bann;
import crm.model.teacherPC.bannedList.BannInterface;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ListPan extends JPanel implements ActionListener {
	private JPanel insertremovePan;
	private JButton removeBut;
	private JPanel endisaplePan;
	private JList list;
	private JButton disableBut;
	private JButton enableBut;
	private JButton addBut;
	ArrayList enableList; ArrayList disableList;
	JFrame gui;
	Manager manager;
	BannInterface bann;
	private EnabledListModel listModel;
	public ListPan(JFrame gui,Manager manager, BannInterface bann){
		this.gui=gui;
		this.manager=manager;
		this.bann = bann;
		initGUI();
	}

	private void initGUI() {
		try {
			{
				BorderLayout thisLayout = new BorderLayout();
				this.setLayout(thisLayout);
				{
					insertremovePan = new JPanel();
					GridLayout insertremovePanLayout = new GridLayout(1, 1);
					insertremovePanLayout.setHgap(5);
					insertremovePanLayout.setVgap(5);
					insertremovePanLayout.setColumns(1);
					insertremovePan.setLayout(insertremovePanLayout);
					this.add(insertremovePan, BorderLayout.SOUTH);
					{
						addBut = new JButton();
						addBut.addActionListener(this);
						addBut.setBorderPainted(false);
						insertremovePan.add(addBut);
						addBut.setText("add");
					}
					{
						removeBut = new JButton();
						removeBut.addActionListener(this);
						removeBut.setBorderPainted(false);
						insertremovePan.add(removeBut);
						removeBut.setText("remove");
					}
				}
				{
					endisaplePan = new JPanel();
					GridLayout endisaplePanLayout = new GridLayout(2, 1);
					endisaplePanLayout.setHgap(5);
					endisaplePanLayout.setVgap(5);
					endisaplePanLayout.setColumns(1);
					endisaplePanLayout.setRows(2);
					endisaplePan.setLayout(endisaplePanLayout);
					this.add(endisaplePan, BorderLayout.EAST);
					{
						enableBut = new JButton();
						enableBut.addActionListener(this);
						enableBut.setBorderPainted(false);
						endisaplePan.add(enableBut);
						enableBut.setText("enable");
					}
					{
						disableBut = new JButton();
						disableBut.addActionListener(this);
						disableBut.setBorderPainted(false);
						endisaplePan.add(disableBut);
						disableBut.setText("disable");
					}
				}
				{
					listModel = new EnabledListModel(bann.getList());
					list = new JList(listModel);
					list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
					list.setCellRenderer(new EnableListRenderer());
					this.add(new JScrollPane(list), BorderLayout.CENTER);
					
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	public void actionPerformed(ActionEvent e) {
		String cmd = e.getActionCommand();
		if (cmd.equals("add")){
			new AddBannDialog(manager, bann, listModel, gui);

//			

		}else if (cmd.equals("remove")){
			if (list.getSelectedValue() != null){
				bann.getList().remove(list.getSelectedValue());
				listModel.fireItemRemoveListener(listModel.getSize()-1);
			}
		}else if (cmd.equals("enable")){
			Object[] bannArr = (list.getSelectedValues());
			
			if (bannArr != null){
				for (int i = 0; i < bannArr.length; i++) {
					Bann bann = (Bann)bannArr[i];
					if (bann != null){
						bann.setEnable(true);
					}
				}
			}
		}else if (cmd.equals("disable")){
			Object[] bannArr = (list.getSelectedValues());
			
			if (bannArr != null){
				for (int i = 0; i < bannArr.length; i++) {
					Bann bann = (Bann)bannArr[i];
					if (bann != null){
						bann.setEnable(false);
					}
				}
			}
		}
		
	}

}
